package com.xnx3.wangmarket.admin.service.impl;

import com.xnx3.j2ee.dao.SqlDAO;
import com.xnx3.wangmarket.admin.entity.Page;
import com.xnx3.wangmarket.admin.entity.Product;
import com.xnx3.wangmarket.admin.entity.Site;
import com.xnx3.wangmarket.admin.entity.SiteColumn;
import com.xnx3.wangmarket.admin.service.ProductManagementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

import static com.xnx3.j2ee.shiro.ShiroFunc.getUserId;

@Service("ProductManagemengImpl")
public class ProductManagemengImpl implements ProductManagementService {

    @Resource
    private SqlDAO sqlDAO;

    @Override
    public void updateProductInfo(Product product, int userid, HttpServletRequest request) {
       sqlDAO.save(product);
    }

    @Override
    public List<SiteColumn> productType(int userid) {
        return sqlDAO.findBySqlQuery("select b.* from site_column a join site_column b on a.id=b.parentid and a.userid="+userid+" and a.type=2",SiteColumn.class);
    }

    @Override
    public SiteColumn pctType( int id) {
        return sqlDAO.findById(SiteColumn.class,id);
    }

    @Override
    public List<Product> queryProductList(int userid) {
        return sqlDAO.findBySqlQuery("select * from product where user_id =" +userid,Product.class);
    }

    @Override
    public Product queryProduct(int id) {
        return sqlDAO.findById(Product.class,id);
    }

    @Override
    public void delProduct(Product product) {
         sqlDAO.delete(product);
    }


    @Override
    public List<Product> productListPage(int page, int count,int userid,Product product) {
        if(product.getProductName()!=null){
            return sqlDAO.findBySqlQuery("select * from product where user_id = "+userid+" and product_name like "+"'%"+product.getProductName()+"%'"+" ORDER BY site_column_id LIMIT " + ((page-1)*10) +" , "+count ,Product.class);
        }
        if(product.getSiteColumnId()!=null){
            return sqlDAO.findBySqlQuery("select * from product where user_id = "+userid+" and site_column_id = "+product.getSiteColumnId() +" ORDER BY site_column_id LIMIT " + ((page-1)*10) +" , "+count ,Product.class);
        }
        if(product.getPrice()!=null&&product.getSiteColumnId()!=null){
            return sqlDAO.findBySqlQuery("select * from product where user_id = "+userid+" and product_name like "+product.getProductName()+"%'"+" and site_column_id= "+product.getSiteColumnId() +" ORDER BY site_column_id LIMIT " + ((page-1)*10) +" , "+count ,Product.class);
        }
        if(product.getProductName()!=null&&product.getSiteColumnId()!=null){
            return sqlDAO.findBySqlQuery("select * from product where user_id = "+userid+" and product_name = "+product.getProductName()+" and site_column_id= "+product.getSiteColumnId()+" ORDER BY site_column_id LIMIT " + ((page-1)*10) +" , "+count ,Product.class);
        }
        if(product.getProductName()==null&&product.getSiteColumnId()==null){
            return sqlDAO.findBySqlQuery("select * from product where user_id = "+userid+" ORDER BY site_column_id LIMIT " + ((page-1)*10) +" , "+count ,Product.class);
        }
            return null;
    }

    @Override
    public int productCount(int userid) {
        List<Product> list = sqlDAO.findBySqlQuery("select * from product where user_id="+userid,Product.class);
        return list.size()+1;
    }
    @Override
    public Page findPage(int page,int count,Product product){

        List<Product> pctList = productListPage(page,count,getUserId(),product);
        int totle = productCount(getUserId());
        Page p = new Page();
        p.setProductList(pctList);
        p.setCurrentPage(page);
        p.setCount(count);
        p.setTotalCount(totle);
        int totlePage = totle%count==0?totle/count:(totle/count)+1;
        p.setTotalPage(totlePage);

        return p;
    }

}
